package com.automannn.seataSamples.business.controller;

import com.automannn.seataSamples.api.dto.BusinessDTO;
import com.automannn.seataSamples.api.response.ObjectResponse;
import com.automannn.seataSamples.business.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author automannn
 * @Date 2022/3/24
 */
@RestController
@Slf4j
@RequestMapping("/business")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    /**
     * 模拟用户购买商品成功的逻辑
     * @param businessDTO 业务信息
     * @return 执行结果
     */
    @PostMapping("/buy")
   public ObjectResponse handleBusinessSuccess(@RequestBody BusinessDTO businessDTO){
        log.info("请求参数：{}", businessDTO.toString());
        return businessService.handleBusinessSuccess(businessDTO);
    }

    /**
     * 模拟用户购买商品失败的逻辑
     * @param businessDTO 业务信息
     * @return 执行结果
     */
    @PostMapping("/buyFail")
    public ObjectResponse handleBusinessFail(@RequestBody BusinessDTO businessDTO){
        log.info("请求参数：{}", businessDTO.toString());
        return businessService.handleBusinessFailWithRollback(businessDTO);
    }
}
